﻿using CommonCater;
using Lufei_CaterBLL;
using Lufei_CaterModel;
using Lufei_CaterUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ItcastCater
{
    public partial class FrmBilling : Form
    {
        public FrmBilling()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 存储DeskID
        /// </summary>
        public int DeskID { get; set; }
        /// <summary>
        /// 给窗体赋值
        /// </summary>
        public void SetValue(object sender,EventArgs e)
        {
            UpdateMemmberInfo<RoomInfo> u = e as UpdateMemmberInfo<RoomInfo>;
            RoomInfo roomInfo = u.obj;
            int id = Convert.ToInt32(u.Type);
            string deskName = u.Name;

            labDeskName.Text = deskName;
            labLittleMoney.Text = roomInfo.RoomMinimunConsume.ToString();
            labRoomType.Text = roomInfo.RoomName;
            this.DeskID = id;
        }


        /// <summary>
        /// 开单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            //1.更改当前桌子的状态  空闲 ---》就餐  ID
            DeskInfoBLL deskInfo = new DeskInfoBLL();
            bool isUpdateDeskState= deskInfo.UpdateDeskStateByID(1,this.DeskID);

            //2.向订单表 插入一条关于【当前桌子】的的订单 orderinfo
            //必须获取插入的新的订单的id    orderID
            OrderInfoBLL orderInfoBLL = new OrderInfoBLL();
            OrderInfo oi = new OrderInfo();
            oi.SubBy = 1;
            oi.SubTime = DateTime.Now;
            oi.BeginTime= DateTime.Now;
            oi.Remark=txtPersonCount.Text+","+txtDescription.Text;
            oi.DelFlag = 0;
            oi.OrderState = 1;
            //返回插入的数据的id
            int orderID= orderInfoBLL.InsertOrderInfo(oi);

            //3.向中间表中 插入一条数据【R_Order_Desk】 deskID orderID
            R_Order_Desk r = new R_Order_Desk();
            r.OrderId = orderID;

            r.DeskId = this.DeskID;

            R_Order_Desk_BLL r_Order_Desk_BLL = new R_Order_Desk_BLL();
            bool isInsert=  r_Order_Desk_BLL.InsertR_Roder_Desk(r);
            if (isUpdateDeskState&& isInsert)
            {
                MessageBox.Show("开单成功!!!!!!!");
                log<FrmBilling>.LogDebug("开单成功");
            }
            else
            {
                MessageBox.Show("开单失败!!!!!!!");
                log<FrmBilling>.LogDebug("开单失败");
            }
            //判断复选框是否被选中
            if (ckbMeal.Checked)
            {
                FrmAddMoney frmAddMoney = new FrmAddMoney();

                //传递参数的编号和餐桌的ID；
                UpdateMemmberInfo<FrmBilling> u2 = new UpdateMemmberInfo<FrmBilling>();
                u2.Name = labDeskName.Text;
                //u2.Type = this.DeskID;  //餐桌  === 》 订单id   去操作订单
                u2.Type = r.OrderId;//订单id

                this.My_Event_FromAddMoney += frmAddMoney.SetValue; //注册事件
                if (this.My_Event_FromAddMoney!=null)
                {
                    this.My_Event_FromAddMoney(this, u2);
                }
                this.Close();//关闭开单页面

                frmAddMoney.Show();
            }
        }



        //注册FrmAddMoney创制传值事件
        private event EventHandler My_Event_FromAddMoney;


    }
}
